#include "serialProcessor.h"

void serialProc(uint8_t* _pAbsis, uint8_t* _pOrdinat, uint8_t* _targetTemp, uint8_t* _actTemp, uint8_t* _bgTemp, uint8_t* _Kp, uint8_t* _Kd, uint8_t* _Ki){
	uint8_t temp = Serial.read();
	//Serial.print(temp);
	enque(&Q,temp);
	//peek(&Q);

	if (temp==DELIMITER){
		C = parseQue (Q);
		Que qOut;
		uint8_t haveReply = 0;

		switch (C.commandType){
			
			case SetTargetTemp:
				#ifdef DEBUG
				Serial.println("SetTargetTemp");
				#endif
				if ((C.value[0]==pAbsis)&&(C.value[1]==pOrdinat)){
					targetTemp = C.value[2];
					haveReply = 1;
					qOut =  makeQue(makeCommand(RepTemp,  actTemp, bgTemp, 0));
					
					#ifdef DEBUG
					Serial.print("Target");
					Serial.println(targetTemp);
					#endif
				}
			break;
			case SetPIDParam: 
				EEPROM.write(KpAdd, C.value[0]);
				EEPROM.write(KiAdd, C.value[1]);
				EEPROM.write(KdAdd, C.value[2]);
				
				Kp = C.value[0];
				Ki = C.value[1];
				Kd = C.value[2];
				
				#ifdef DEBUG
				Serial.println("SetPIDParam \n KP Ki Kd");
				Serial.print(Kp);Serial.print(" - ");
				Serial.print(Ki);Serial.print(" - ");
				Serial.print(Kd);Serial.print(" - ");
				#endif					
						
			break;
			case SetPlateID:
				if (C.value[0] != 255 && C.value[1] != 255){
					EEPROM.write(absisAdd, C.value[0]);
					EEPROM.write(ordinAdd, C.value[1]);
					
					pAbsis = C.value[0];
					pOrdinat = C.value[1];
					
					#ifdef DEBUG
					Serial.println("SetPlateID");
					#endif
				}
				
				haveReply = 1;
				qOut = makeQue(makeCommand(RepPlateID,  pAbsis, pOrdinat, 0));
				
				#ifdef DEBUG
				Serial.print("plateID is ");
				Serial.print(pAbsis);Serial.print(" , ");
				Serial.print(pOrdinat);Serial.println("");
				#endif
			break;
			}

		if (haveReply) {
			for (int i=0; i<6; ++i){
				char temp2 = deque(&qOut);
				Serial.write(temp2);
			}
		}
	}
}


